#include "server.hpp"

#define HOST "127.0.0.1"
#define USER "lyw"
#define PASS "w53146"
#define DBNAME "gobang"
#define PORT 3306

void mysql_test()
{
    MYSQL* mysql = mysql_util::mysql_create(HOST, USER, PASS, DBNAME, PORT);
    const char* sql = "insert stu value (null, '小明');";
    bool ret = mysql_util::mysql_exec(mysql, sql);
    if (!ret)
        return;
    mysql_util::mysql_destroy(mysql);
}

void json_test()
{
    Json::Value root;
    root["姓名"] = "小明";
    root["年龄"] = 18;
    root["成绩"].append(90); 
    root["成绩"].append(91.11);
    root["成绩"].append(63);
    std::string body;
    json_util::serialize(root, body);
    DLOG("%s", body.c_str());

    Json::Value val;
    json_util::unserialize(body, val);
    std::cout << "姓名:" << val["姓名"].asString() << std::endl;
    std::cout << "年龄:" << val["年龄"].asInt() << std::endl;
    int sz = val["成绩"].size();
    for (int i = 0; i < sz; i++)
    {
        std::cout << "成绩:" << val["成绩"][i].asFloat() << std::endl;
    }
}

void string_test()
{
    std::string str = "abcsd/?sadawd/?/?ac/?aa/?/?/?";
    std::vector<std::string> vs;
    string_util::split(str, "/?", vs);
    for (auto s : vs)
        DLOG("%s", s.c_str());
}

void file_test()
{
    std::string filename = "./Makefile";
    std::string body;
    file_util::read(filename, body);
    DLOG("%s", body.c_str());
}

void db_test()
{
    user_table ut(HOST, USER, PASS, DBNAME, PORT);
    Json::Value user;
    user["username"] = "小明";
    // user["password"] = "123456";
    bool ret = ut.insert(user);
    // bool ret = ut.lose(1);
    // std::string body;
    // json_util::serialize(user, body);
    // DLOG("%s", body.c_str());
}

void online_test()
{
    online_manager om;
    wsserver_t::connection_ptr conn;
    uint64_t uid = 6;
    om.enter_game_room(uid, conn);
    if (om.is_in_game_room(uid))
        DLOG("in game room");
    else
        DLOG("not in game room");
    om.exit_game_room(uid);
    if (om.is_in_game_room(uid))
        DLOG("in game room");
    else
        DLOG("not in game room");
}

void room_test()
{
    user_table ut(HOST, USER, PASS, DBNAME, PORT);
    online_manager om;
    room_manager rm(&ut, &om);
    room_ptr rp = rm.create_room(10, 20);
}

void matcher_test()
{
    user_table ut(HOST, USER, PASS, DBNAME, PORT);
    online_manager om;
    room_manager rm(&ut, &om);
    room_ptr rp = rm.create_room(10, 20);
    matcher_manager mc(&rm, &ut, &om);
}

void server_test()
{
    gobang_server server(HOST, USER, PASS, DBNAME, PORT);
    server.start(8080);
}

int main()
{
    server_test();

    return 0;
}